﻿#region License
///License Begin
///This program has been developed by: Rodolfo Grave de Peralta, 
///Copyright (C) 2007 Rodolfo Grave de Peralta
///This program is free software; you can redistribute it and/or modify
///it under the terms of the GNU General Public License as published by
///the Free Software Foundation; either version 2 of the License, or
///(at your option) any later version.
///
///This program is distributed in the hope that it will be useful,
///but WITHOUT ANY WARRANTY; without even the implied warranty of
///MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
///GNU General Public License for more details.
///
///You should have received a copy of the GNU General Public License
///along with this program; if not, write to the Free Software
///Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
///License End
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;

using Db4objects.Db4o;

namespace TEAM.TimeTracking.BackEndService
{
    public partial class MainService : ServiceBase
    {

        protected IObjectServer Database;

        public MainService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            if (args.Length != 2)
            {
                EventLog.WriteEntry("TEAM's TimeTracking Service", "Incorrect arguments. Right params are: database.yap portNumber", EventLogEntryType.Error);
            }
            else
            {
                string fileName = args[0];
                int port;
                if (int.TryParse(args[1], out port))
                {
                    Db4oFactory.Configure().ActivationDepth(1000);
                    Db4oFactory.Configure().UpdateDepth(1000);
                    Database = Db4oFactory.OpenServer(fileName, port);
                    Database.GrantAccess("dbuser", "dbuserpwd");
                }
                else
                {
                    EventLog.WriteEntry("TEAM's TimeTracking Service", "Incorrect arguments. portNumber must be a number", EventLogEntryType.Error);
                }
            }
        }

        protected override void OnStop()
        {
            Database.Close();
        }
    }
}
